package cn.edu.zut.internship.service.impl.tutor;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.iservice.notify.NoticeSV;
import cn.edu.zut.internship.iservice.notify.NotifySV;
import cn.edu.zut.internship.iservice.student.StudentSV;
import cn.edu.zut.internship.iservice.tutor.TutorSV;
import cn.edu.zut.internship.mapper.setting.SettingMapper;
import cn.edu.zut.internship.mapper.student.StudentMapper;
import cn.edu.zut.internship.mapper.teach.TeachMapper;
import cn.edu.zut.internship.mapper.tutor.TutorMapper;
import cn.edu.zut.internship.service.impl.result.ResultSVImpl;
import cn.edu.zut.internship.utils.ExcelUtils;
import cn.edu.zut.internship.utils.FileNameUtil;
import cn.edu.zut.internship.utils.ThreadPoolUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TutorSVImpl implements TutorSV {
    @Autowired
    TutorMapper tutorMapper;
    @Autowired
    TeachMapper teachMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    NotifySV notifySV;
    @Autowired
    StudentSV studentSV;
    @Autowired
    SettingMapper settingMapper;
    @Override
    public Result getMyStudent(Map params) {
        Result result = new Result();
        //获取当前学期
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        params.put("semester",nowSemester);
        //返回数据库的结果
        List<Map> myStudent = tutorMapper.getMyStudent(params);
        //返回数据总数
        int total = tutorMapper.getMyStudentCount(params);
        //将列表总数放到result的bean中
        result.getBean().put("total",total);
        //将数据库的结果放到result中
        result.setBeans(myStudent);
        return result;
    }

    @Override
    public Result choiceStudent(Map params) {
        Result result = new Result();
        String teachName = (String) params.get("teachName");
        //获取当前学期
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        List<Map> students = (List<Map>) params.get("choices");
        if(!CollectionUtils.isEmpty(students)){
            for(Map student:students){
                //给学生绑定指导教师
                student.put("teachAccount",params.get("teachAccount"));
                student.put("semester",nowSemester);
                tutorMapper.addStudentTeach(student);
                //给绑定的学生发送提醒消息
                Map notify = new HashMap();
                notify.put("sendAccount",params.get("teachAccount"));
                notify.put("sendType",0);
                notify.put("notifyType",0);
                notify.put("content","教师"+teachName+"已绑定为你的指导教师");
                notify.put("targetAccount",student.get("studentAccount"));
                notify.put("targetType",0);
                notify.put("creatAccount",params.get("teachAccount"));
                notifySV.addNotify(notify);
            }
        }
        return result;
    }

    @Override
    public Result autuDistribut(Map params) {
        Result result = new Result();
        //启动线程分配指导教师
        ThreadPoolUtil.getThreadPool().execute(new task(params));
        return result;
    }

    @Override
    public Result getTeachList(Map params) {
        Result result = new Result();
        //获取当前学期，只查询当前学期的数据
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        List<Map> teachList = teachMapper.selectTeach(params);
        int total = teachMapper.selectTeachCount(params);
        //查询教师分配学生总数
        Map map = new HashMap();
        map.put("semester",nowSemester);
        for(Map teach:teachList){
            map.put("teachAccount",teach.get("userAccount"));
            teach.put("studentCount",tutorMapper.getMyStudentCount(map));
        }

        result.setBeans(teachList);
        result.getBean().put("total",total);
        return result;
    }

    @Override
    public Result getDistributData(Map params) {
        Result result = new Result();
        //获取当前学期，只查询当前学期的数据
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        params.put("semester",nowSemester);
        //获取未分配教师的学生总人数
        int studentCount = studentMapper.getNoTeachStudentCount(params);
        //获取教师总人数
        int teachCount = teachMapper.selectTeachCount(params);
        //获取已分配导师学生总人数
        int studentTutorCount = tutorMapper.getStudentListCount(params);
        //平均应分配学生数
        int average = 0;
        if(teachCount>0){
            average = studentTutorCount+studentCount/teachCount;
        }
        result.getBean().put("studentCount",studentCount);
        result.getBean().put("teachCount",teachCount);
        result.getBean().put("studentTutorCount",studentTutorCount);
        result.getBean().put("allStudentCount",studentTutorCount+studentCount);
        result.getBean().put("average",average);
        return result;
    }

    @Override
    public Result noChoice(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        tutorMapper.noChoice(params);
        //给解除指导的学生发送提醒消息
        Map notify = new HashMap();
        notify.put("sendAccount",params.get("userAccount"));
        notify.put("sendType",0);
        notify.put("notifyType",0);
        notify.put("content","教师"+userInfo.getUserName()+"已解除与你的指导关系");
        notify.put("targetAccount",params.get("studentAccount"));
        notify.put("targetType",0);
        notify.put("creatAccount",params.get("userAccount"));
        notifySV.addNotify(notify);
        return result;
    }
    class task extends  Thread{
        private Map params;
        public task(Map params) {
            this.params=params;
        }
        @Override
        public void run() {
            //获取当前学期，只查询当前学期的数据
            Map semesterParams = new HashMap();
            Map semester = settingMapper.getSetting("currentSemester");
            String nowSemester = (String) semester.get("settingValue");
            semesterParams.put("semester",nowSemester);
            //当前学期学生总数
            int studentCount = studentMapper.selectStudentCount(semesterParams);
            //当前学期未分配教师学生总数
            List<Map> noTeachStudent = studentMapper.getNoTeachStudent(semesterParams);
            List<Map> teachList = teachMapper.selectTeach(new HashMap());
            //若教师或者未分配学生数量为零就停止执行
            if(CollectionUtils.isEmpty(noTeachStudent)||CollectionUtils.isEmpty(teachList)){
                return ;
            }
            int average = studentCount/teachList.size();
            int noTeachStudentNum = noTeachStudent.size();
            //为每名教师分配平均数量的学生
            for(Map teach:teachList){
                Map map = new HashMap();
                map.put("teachAccount",teach.get("userAccount"));
                //当前学期该教师已分配学生总数
                map.put("semester",nowSemester);
                int myStudentCount = tutorMapper.getMyStudentCount(map);
                //若该教师已分配的学生小于应分配学生数
                if(average>myStudentCount){
                    Map tutor = new HashMap();
                    tutor.put("teachAccount",teach.get("userAccount"));
                    tutor.put("teachName",teach.get("userName"));
                    List<Map> choices = new ArrayList<>();
                    //为此教师继续分配学生直至已分配数达到应分配数
                    for(int i = 0;i<average-myStudentCount;i++){
                        Map choice = new HashMap();
                        choice.put("teachAccount",teach.get("userAccount"));
                        choice.put("schoolYear",nowSemester);
                        choice.put("studentAccount",noTeachStudent.get(--noTeachStudentNum).get("studentAccount"));
                        choices.add(choice);
                    }
                    tutor.put("choices",choices);
                    choiceStudent(tutor);
                }
            }
            //处理余数部分学生分配
            int i = 0;
            for(int n = noTeachStudentNum;n > 0;n -- ){
                Map teach = teachList.get(i++);
                Map tutor = new HashMap();
                tutor.put("teachAccount",teach.get("userAccount"));
                tutor.put("teachName",teach.get("userName"));
                List<Map> choices = new ArrayList<>();
                Map choice = new HashMap();
                choice.put("teachAccount",teach.get("userAccount"));
                choice.put("semester",nowSemester);
                choice.put("studentAccount",noTeachStudent.get(--noTeachStudentNum).get("studentAccount"));
                choices.add(choice);
                tutor.put("choices",choices);
                choiceStudent(tutor);
            }

        }
    }

}
